package com.hrb.douban.activtity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.hrb.douban.CustomApplication;
import com.hrb.douban.R;
import com.hrb.douban.activtity.base.BaseActivity;
import com.hrb.douban.adapter.CollectMovieAdapter;
import com.hrb.douban.adapter.CollectPageAdapter;
import com.hrb.douban.mview.CustomTitleBar;
import com.hrb.douban.record.FilmDetailRecord;
import com.hrb.douban.record.PersonRecord;
import com.hrb.douban.utils.ACache;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CollectPageActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.custom_title_bar)
    CustomTitleBar customTitleBar;
    @BindView(R.id.movie_title)
    TextView movieTitle;
    @BindView(R.id.person_title)
    TextView personTitle;
    @BindView(R.id.collect_recycle)
    RecyclerView collectRecycle;
    @BindView(R.id.movie_collect_recycle)
    RecyclerView movieCollectRecycle;
    private ACache cacheUtil;
    private String titleName;
    private ArrayList<PersonRecord> personlist = new ArrayList<PersonRecord>();
    private ArrayList<FilmDetailRecord> films = new ArrayList<FilmDetailRecord>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collect_page);
        ButterKnife.bind(this);
        titleName= getIntent().getStringExtra("title");
        customTitleBar.setTitleClickListener(this);
        customTitleBar.getMtitle().setText(titleName);
        movieTitle.setOnClickListener(this);
        personTitle.setOnClickListener(this);
        cacheUtil = ACache.get(CustomApplication.getContext());
        try {
            if("收藏".equals(titleName)){
                if((ArrayList<FilmDetailRecord>)cacheUtil.getAsObject("filmlistCollect")!=null){
                    films=(ArrayList<FilmDetailRecord>)cacheUtil.getAsObject("filmlistCollect");
                }
                if((ArrayList<PersonRecord>)cacheUtil.getAsObject("personlistCollect")!=null){
                    personlist=(ArrayList<PersonRecord>)cacheUtil.getAsObject("personlistCollect");
                }
            }else{
                if((ArrayList<FilmDetailRecord>)cacheUtil.getAsObject("filmlist")!=null){
                    films=(ArrayList<FilmDetailRecord>)cacheUtil.getAsObject("filmlist");
                }
                if((ArrayList<PersonRecord>)cacheUtil.getAsObject("personlist")!=null){
                    personlist=(ArrayList<PersonRecord>)cacheUtil.getAsObject("personlist");
                }
            }
        } catch (Exception e) {
            Log.d("CollectPageActivity","缓存历史获取失败");
            e.printStackTrace();
        }

        LinearLayoutManager llm=new LinearLayoutManager(this);
        movieCollectRecycle.setLayoutManager(llm);
        CollectMovieAdapter collectMovieAdapter=new CollectMovieAdapter(films);
        movieCollectRecycle.setAdapter(collectMovieAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        collectRecycle.setLayoutManager(linearLayoutManager);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_bar_left:
                finish();
                break;
            case R.id.movie_title:
                movieTitle.setBackgroundResource(R.drawable.swiper_bottom_line_shape_selected);
                personTitle.setBackgroundResource(R.drawable.swiper_bottom_line_shape_unselected);
                movieCollectRecycle.setVisibility(View.VISIBLE);
                collectRecycle.setVisibility(View.GONE);
                CollectMovieAdapter collectMovieAdapter=new CollectMovieAdapter(films);
                movieCollectRecycle.setAdapter(collectMovieAdapter);
                break;
            case R.id.person_title:
                movieTitle.setBackgroundResource(R.drawable.swiper_bottom_line_shape_unselected);
                personTitle.setBackgroundResource(R.drawable.swiper_bottom_line_shape_selected);
                movieCollectRecycle.setVisibility(View.GONE);
                collectRecycle.setVisibility(View.VISIBLE);
                CollectPageAdapter adapter = new CollectPageAdapter(personlist);
                collectRecycle.setAdapter(adapter);
                break;
        }
    }
}
